﻿using BLL;
using Model;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace UI
{
    public partial class OrderAddProductsForm : Form
    {
        private ProductsBLL _productsBLL = new ProductsBLL();
        private OrderInfoBLL _orderInfoBLL = new OrderInfoBLL();
        private OrderDTO _orderDTO = new OrderDTO();

        public OrderAddProductsForm()
        {
            InitializeComponent();
        }


        public void GetData(object obj, EventArgs e) 
        {
            _orderDTO = e as OrderDTO;

            label9.Text = _orderDTO.DeskName;
            label10.Text = _orderDTO.RoomName;
            
        }

        public void InitData()
        {
            DataTable dataTable1 = _orderInfoBLL.SelectProductByOrder(_orderDTO.OrderId);
            dgvROrderProduct.DataSource = dataTable1;

            DataTable dataTable2 = _productsBLL.SelectProductsInfo();
            dgvProductInfo.DataSource = dataTable2;

            int count = 0, price = 0;
            _orderInfoBLL.SelectOrderCountAndPrice(_orderDTO, out count, out price);
            lblCount.Text = count.ToString();
            lblMoney.Text = price.ToString();

        }

        // 初始化树状视图
        public void InitTreeViewData()
        {
            //获取商品类别信息表
            DataTable dataTable1 = _productsBLL.SelectProductsCategory();

            //获取所有商品类别id
            foreach(DataRow dataRow in dataTable1.Rows)
            {
                //创建数节点充当一级类别
                TreeNode treeNode = new TreeNode(dataRow[1].ToString());
                

                //根据商品类别id查询商品
                DataTable dataTable2 = _productsBLL.SelectProductsInfoByID(Convert.ToInt32(dataRow[0]));
                //获取查询到的所有商品
                foreach(DataRow dataRow1 in dataTable2.Rows)
                {
                    //在每个树节点下添加子节点
                    treeNode.Nodes.Add(dataRow1[2].ToString());
                }

                //往Tab里面添加创建好的TreeView信息
                tvCategory.Nodes.Add(treeNode);

            }

            
        }

        private void OrderAddProductsForm_Load(object sender, EventArgs e)
        {
            InitData();
            InitTreeViewData();
        }

        //双击添加商品
        private void dgvProductInfo_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            //准备好需要的数据
            Order_ProductDTO order_ProductDTO = new Order_ProductDTO();
            order_ProductDTO.OrderId = _orderDTO.OrderId;
            order_ProductDTO.ProductId = Convert.ToInt32(dgvProductInfo.SelectedRows[0].Cells[0].Value);
            order_ProductDTO.SubTime = DateTime.Now;
            order_ProductDTO.ProCount = 1;
            
            //添加商品,BLL封装了逻辑判断
            int v = _orderInfoBLL.AddOrderProducts(order_ProductDTO);
            if (v > 0)
            {
                MessageBox.Show("添加成功");
                //刷新添加到的数据
                InitData();
            }
            else
            {
                MessageBox.Show("添加失败");
            }



        }

        private void btnDeleteRorderPro_Click(object sender, EventArgs e)
        {
            //退订商品的功能
            //如果商品数量大于1则-1，否则商品数量=1则delflag = 1
            int orderId = _orderDTO.OrderId;
            int productId = Convert.ToInt32(dgvROrderProduct.SelectedRows[0].Cells[0].Value);
            int proCount = Convert.ToInt32(dgvROrderProduct.SelectedRows[0].Cells[3].Value);

            int v = _orderInfoBLL.UpdateProCountByOrderId(orderId, productId);

            if(v > 0)
            {
                //刷新数据
                InitData();
            }

        }
    }
}
